<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateOrganizationAPIRequest;
use App\Http\Requests\API\UpdateOrganizationAPIRequest;
use App\Models\Organization;
use App\Repositories\OrganizationRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class OrganizationController
 * @package App\Http\Controllers\API
 */

class OrganizationAPIController extends AppBaseController
{
    /** @var  OrganizationRepository */
    private $organizationRepository;

    public function __construct(OrganizationRepository $organizationRepo)
    {
        $this->organizationRepository = $organizationRepo;
    }

    /**
     * Display a listing of the Organization.
     * GET|HEAD /organizations
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->organizationRepository->pushCriteria(new RequestCriteria($request));
        $this->organizationRepository->pushCriteria(new LimitOffsetCriteria($request));
        $organizations = $this->organizationRepository->all()->toTree();

        return $this->sendResponse($organizations->toArray(), '获取组织架构成功');
    }

    /**
     * Store a newly created Organization in storage.
     * POST /organizations
     *
     * @param CreateOrganizationAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateOrganizationAPIRequest $request)
    {
        $input = $request->all();

        //查询父节点
        $parent = Organization::find($request->get('parent_id',0));
        $organization= $organization = $this->organizationRepository->create($input);

        if($parent) {
            $organization->appendToNode($parent)->save();
        }

        return $this->sendResponse($organization->toArray(), '组织添加成功');
    }

    /**
     * Update the specified Organization in storage.
     * PUT/PATCH /organizations/{id}
     *
     * @param  int $id
     * @param UpdateOrganizationAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateOrganizationAPIRequest $request)
    {
        $input = $request->all();

        /** @var Organization $organization */
        $organization = $this->organizationRepository->findWithoutFail($id);

        if (empty($organization)) {
            return $this->sendError('组织结构不存在');
        }

        //查询父节点
        $parent = Organization::find($request->get('parent_id',0));

        $organization = $this->organizationRepository->update($input, $id);

        if($parent) {
            $organization->appendToNode($parent)->save();
        }

        return $this->sendResponse($organization->toArray(), '组织结构更新成功');
    }

    /**
     * Remove the specified Organization from storage.
     * DELETE /organizations/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var Organization $organization */
        $organization = $this->organizationRepository->findWithoutFail($id);

        if (empty($organization)) {
            return $this->sendError('组织结构不存在');
        }

        $organization->delete();

        return $this->sendResponse($id, '组织结构删除成功');
    }
}
